# Merise python parser and models generators.
#
# @autor: Patrice Ferlet <metal3d@gmail.com>
# @version 0.0.2
# @licence Gnu/GPL v3

# $Id: sql.py 49 2010-08-01 13:43:24Z Metal3d $
# $HeadURL: http://merisier.googlecode.com/svn/tags/0.0.2-beta1/src/merise/sql.py $


"""
Sql generator for merise module
It generates SQL file from merise file
"""
class Sql:
    def __init__(self, parser):
        """
        Initialize Sql generator from merise.parser.
        merise.parser.options.sqlbackend set Backend to implement
        """
        self.parser = parser
    
    def generate(self):
        """
        Generate SQL file based on backend given in parser options
        """
        bnd = False
        for k in self.parser.virtuals:
			del self.parser.defs[k]
        
        if self.parser.options.sqlbackend == 'mysql':
            from .backend.mysql import Generator
            bnd = True
        if self.parser.options.sqlbackend == 'postgre':
            from .backend.postgre import Generator
            bnd = True
        if self.parser.options.sqlbackend == 'sqlite':
            from .backend.sqlite import Generator
            bnd = True

        if not bnd or self.parser.options.sqlbackend == 'all':
            from .backend import mysql, postgre, sqlite
            
            g = mysql.Generator(self.parser)
            g.generate()
            
            g = postgre.Generator(self.parser)
            g.generate()
            
            g = sqlite.Generator(self.parser)
            g.generate()
            
        else:
            g = Generator(self.parser);
            g.generate()
            


